package com.ruoyi.web.controller.module.estate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.alibaba.excel.util.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.EstateCustomerCallRecord;
import com.ruoyi.system.domain.module.estate.EstateCustomerCallRecordImg;
import com.ruoyi.system.service.module.estate.IEstateCustomerCallRecordImgService;
import com.ruoyi.system.service.module.estate.IEstateCustomerCallRecordService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 客服跟业主电话沟通记录Controller
 *
 * @author ruoyi
 * @date 2025-09-25
 */
@Validated
@Api(value = "客服跟业主电话沟通记录控制器", tags = {"客服跟业主电话沟通记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/customer/call/record")
public class EstateCustomerCallRecordController extends BaseController {

    private final IEstateCustomerCallRecordService estateCustomerCallRecordService;
	private final IEstateCustomerCallRecordImgService estateCustomerCallRecordImgService;

	@Value("${ruoyi.imgUrlOss}")
	private String imgUrlOss;

    /**
     * 查询客服跟业主电话沟通记录列表
     */
    @ApiOperation("查询客服跟业主电话沟通记录列表")
    @PreAuthorize("@ss.hasPermi('module:customerCallRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateCustomerCallRecord> list(@Validated EstateCustomerCallRecord estateCustomerCallRecord) {
        QueryWrapper<EstateCustomerCallRecord> qw = new QueryWrapper<>();
		if(estateCustomerCallRecord.getRoomNo() != null){
			qw.like("room_no",estateCustomerCallRecord.getRoomNo());
		}
		if(estateCustomerCallRecord.getCallPhone() != null){
			qw.like("call_phone",estateCustomerCallRecord.getCallPhone());
		}
		if(estateCustomerCallRecord.getEstateId() != null){
			qw.eq("estate_id",estateCustomerCallRecord.getEstateId());
		}
		if(estateCustomerCallRecord.getState() != null){
			qw.eq("state",estateCustomerCallRecord.getState());
		}
		qw.orderByDesc("id");

        TableDataInfo<EstateCustomerCallRecord> td = PageUtils.buildDataInfo(estateCustomerCallRecordService.page(PageUtils.buildPage(),qw));
        List<EstateCustomerCallRecord> list = td.getRows();
		for (EstateCustomerCallRecord estateCustomerCallRecord1 : list) {
			List<String> imgListArray = new ArrayList<>();
			List<EstateCustomerCallRecordImg> imgList = estateCustomerCallRecordImgService.list(new QueryWrapper<EstateCustomerCallRecordImg>().eq("record_id",estateCustomerCallRecord1.getId()));
			for (EstateCustomerCallRecordImg estateCustomerCallRecordImg : imgList) {
				imgListArray.add(imgUrlOss + estateCustomerCallRecordImg.getImgUrl());
			}
			estateCustomerCallRecord1.setRecordImgList(imgListArray);
		}
        return td;
    }

    /**
     * 导出客服跟业主电话沟通记录列表
     */
    @ApiOperation("导出客服跟业主电话沟通记录列表")
    @PreAuthorize("@ss.hasPermi('module:customerCallRecord:export')")
    @Log(title = "客服跟业主电话沟通记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateCustomerCallRecord> export(@Validated EstateCustomerCallRecord estateCustomerCallRecord) {
		QueryWrapper<EstateCustomerCallRecord> qw = new QueryWrapper<>();
		if(estateCustomerCallRecord.getRoomNo() != null){
			qw.like("room_no",estateCustomerCallRecord.getRoomNo());
		}
		if(estateCustomerCallRecord.getCallPhone() != null){
			qw.like("call_phone",estateCustomerCallRecord.getCallPhone());
		}
		if(estateCustomerCallRecord.getEstateId() != null){
			qw.eq("estate_id",estateCustomerCallRecord.getEstateId());
		}
		if(estateCustomerCallRecord.getState() != null){
			qw.eq("state",estateCustomerCallRecord.getState());
		}
		qw.orderByDesc("id");
        List<EstateCustomerCallRecord> list = estateCustomerCallRecordService.list(qw);
		for (EstateCustomerCallRecord estateCustomerCallRecord1 : list) {
			Date time = estateCustomerCallRecord1.getCallTime();
			estateCustomerCallRecord1.setCallTimeStr(DateUtils.format(time,"yyyy-MM-dd HH:mm:ss"));
			if(estateCustomerCallRecord1.getState() == 0){
				estateCustomerCallRecord1.setStateStr("待处理");
			} else if(estateCustomerCallRecord1.getState() == 1){
				estateCustomerCallRecord1.setStateStr("跟进中");
			} else if(estateCustomerCallRecord1.getState() == 2){
				estateCustomerCallRecord1.setStateStr("处理完成");
			}
		}
        ExcelUtil<EstateCustomerCallRecord> util = new ExcelUtil<>(EstateCustomerCallRecord.class);
        return util.exportExcel(list, "客服跟业主电话沟通记录");
    }

    /**
     * 获取客服跟业主电话沟通记录详细信息
     */
    @ApiOperation("获取客服跟业主电话沟通记录详细信息")
    @PreAuthorize("@ss.hasPermi('module:customerCallRecord:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateCustomerCallRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateCustomerCallRecord estateCustomerCallRecord = estateCustomerCallRecordService.getById(id);
		if(estateCustomerCallRecord != null){
			List<String> imgListArray = new ArrayList<>();
			List<EstateCustomerCallRecordImg> imgList = estateCustomerCallRecordImgService.list(new QueryWrapper<EstateCustomerCallRecordImg>().eq("record_id", estateCustomerCallRecord.getId()));
			for (EstateCustomerCallRecordImg estateCustomerCallRecordImg : imgList) {
				imgListArray.add(imgUrlOss + estateCustomerCallRecordImg.getImgUrl());
			}
			estateCustomerCallRecord.setRecordImgList(imgListArray);
		}
        return AjaxResult.success(estateCustomerCallRecord);
    }

    /**
     * 新增客服跟业主电话沟通记录
     */
    @ApiOperation("新增客服跟业主电话沟通记录")
    @PreAuthorize("@ss.hasPermi('module:customerCallRecord:add')")
    @Log(title = "客服跟业主电话沟通记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateCustomerCallRecord estateCustomerCallRecord) {
        return toAjax(estateCustomerCallRecordService.save(estateCustomerCallRecord) ? 1 : 0);
    }

    /**
     * 修改客服跟业主电话沟通记录
     */
    @ApiOperation("修改客服跟业主电话沟通记录")
    @PreAuthorize("@ss.hasPermi('module:customerCallRecord:edit')")
    @Log(title = "客服跟业主电话沟通记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateCustomerCallRecord estateCustomerCallRecord) {
		boolean bl = estateCustomerCallRecordService.updateById(estateCustomerCallRecord);
		if(bl){
			List<String> imgList = estateCustomerCallRecord.getUploadImgList();
			for (String img : imgList) {
				EstateCustomerCallRecordImg estateCustomerCallRecordImg = new EstateCustomerCallRecordImg();
				estateCustomerCallRecordImg.setImgUrl(img);
				estateCustomerCallRecordImg.setRecordId(estateCustomerCallRecord.getId());
				estateCustomerCallRecordImg.setCreateTime(new Date());
				estateCustomerCallRecordImgService.save(estateCustomerCallRecordImg);
			}
		}
        return toAjax(bl ? 1 : 0);
    }

    /**
     * 删除客服跟业主电话沟通记录
     */
    @ApiOperation("删除客服跟业主电话沟通记录")
    @PreAuthorize("@ss.hasPermi('module:customerCallRecord:remove')")
    @Log(title = "客服跟业主电话沟通记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateCustomerCallRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
